package com.tjshop.android;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tjshop.android.util.MiscUtils;

/**
 * 帮助界面。<br/>
 * 使用ViewPager+Fragment，并且创建Fragment时使用Bundle来传递参数。<br/>
 * 这种方式的好处是：<br/>
 * 
 * <i> This is best for paging across a collection of objects for which the
 * number of pages is undetermined. It destroys fragments as the user navigates
 * to other pages, minimizing memory usage. </i> <br/>
 * 即，用户切换到其它Fragment时，系统会自动销毁之前的Fragment以减小内存使用。<br/>
 * 参考：<a href=
 * "http://developer.android.com/training/implementing-navigation/lateral.html"
 * >Creating Swipe Views with Tabs</a>
 * 
 * @author Ke Shang
 * @since 2014-09-26
 * 
 */
public class HelpActivity extends FragmentActivity implements OnClickListener,
		Callback {

	/**
	 * Pager的Size
	 */
	static final int PAGER_SIZE = R.drawable.help_image_04
			- R.drawable.help_image_01 + 1;

	// private Editor editor;
	// private SharedPreferences mPreferences;
	private static Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_help);
		mHandler = new Handler(this);
		SharedPreferences sp = getPreferences(MODE_PRIVATE);
		if (true) {
			// if(!sp.getBoolean("bFirst", true)){
			jump2MainActivity();
		}
		initViews();
		initFlow();
	}

	private void initViews() {
		// mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		// editor = preferences.edit();

		HelpPagerAdapter pagerAdapter = new HelpPagerAdapter(
				getSupportFragmentManager());
		ViewPager pager = (ViewPager) findViewById(R.id.activity_help_pager);
		pager.setAdapter(pagerAdapter);

		// CirclePageIndicator indicator = (CirclePageIndicator)
		// findViewById(R.id.activity_help_indicator);
		// indicator.setFillColor(0xFFFF0000);
		// indicator.setPageColor(0xFFFFFFFF);
		// indicator.setStrokeWidth(0);
		// indicator.setViewPager(pager);

		// findViewById(R.id.activity_help_btn_start).setOnClickListener(this);
		// // "开始使用"按钮
	}

	private void initFlow() {
		// boolean isFirstUser = mPreferences.getBoolean("isFirstUser", true);//
		// 是否第一次使用软件，TODO
		// 可以专门写一个操作Preference的Helper类
		// if (!isFirstUser) {
		// jump2MainActivity();
		// }
	}

	private void jump2MainActivity() {
		MiscUtils.startActivity(this, MainActivity.class, getIntent());
		finish(); // 关闭当前Activity
	}

	@Override
	public void onBackPressed() {
		jump2MainActivity();
	}

	@Override
	public void onClick(View v) {
		// if (v.getId() == R.id.activity_help_btn_start) {
		// jump2MainActivity();
		//
		// // editor.putBoolean("isFirstUser", false);
		// // editor.commit();
		// }
	}

	// Since this is an object collection, use a FragmentStatePagerAdapter,
	// and NOT a FragmentPagerAdapter.
	public class HelpPagerAdapter extends FragmentStatePagerAdapter {
		public HelpPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			Fragment fragment = new HelpPagerFragment();

			Bundle args = new Bundle();
			args.putInt(HelpPagerFragment.ARG_IMAGE, R.drawable.help_image_01
					+ i);
			fragment.setArguments(args);

			return fragment;
		}

		@Override
		public int getCount() {
			return PAGER_SIZE;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return "OBJECT " + (position + 1);
		}
	}

	// Instances of this class are fragments representing a single
	// object in our collection.
	public static class HelpPagerFragment extends Fragment {
		public static final String ARG_IMAGE = "image";

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.activity_help_fragment_pager, container, false);

			Bundle args = getArguments();
			int imageId = args.getInt(ARG_IMAGE);
			((ImageView) rootView
					.findViewById(R.id.activity_help_fragment_pager_item_image))
					.setImageResource(imageId);

			if (imageId == R.drawable.help_image_04) {
				rootView.findViewById(
						R.id.activity_help_fragment_pager_item_start_container)
						.setVisibility(View.VISIBLE);

				rootView.findViewById(
						R.id.activity_help_fragment_pager_item_enter)
						.setOnClickListener(mOnClickListener);
			} else {
				rootView.findViewById(
						R.id.activity_help_fragment_pager_item_start_container)
						.setVisibility(View.GONE);
			}

			return rootView;
		}

		OnClickListener mOnClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				mHandler.sendEmptyMessage(10);
			}

		};
	}

	@Override
	public boolean handleMessage(Message msg) {
		SharedPreferences sp = getPreferences(MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean("bFirst", false);
		editor.commit();
		jump2MainActivity();
		return true;
	}

}
